package com.xzl.time.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xzl.time.exceptions.MyException;
import com.xzl.time.mapper.ArticleMapper;
import com.xzl.time.mapper.CommentMapper;
import com.xzl.time.pojo.Comment;
import com.xzl.time.pojo.Notice;
import com.xzl.time.pojo.vo.AddCommentParamVo;
import com.xzl.time.service.ICommentService;
import com.xzl.time.utils.MyEnum;
import com.xzl.tool.util.IdWorker;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * @describe ：评论实现
 * @PageLocation:
 * @Auther xzl
 * @Date 2021/12/29 15:39
 */
@Service
public class CommentServiceImpl implements ICommentService {
    @Autowired
    private IdWorker idWorker;

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public Comment findById(String id) {
        Comment byId = commentMapper.selectById(id);
        return byId;
    }

    @Override
    public List<Comment> findAll() {
        return commentMapper.selectList(null);
    }

    @Override
    public AddCommentParamVo save(AddCommentParamVo comment) {
        if (StringUtils.isBlank(comment.getUserName())
                || StringUtils.isBlank(comment.getContent())
                || StringUtils.isBlank(comment.getArticleId()))
            throw new MyException(500, "参数异常！");

        //初始化数据
        String id = idWorker.nextId() + "";
        comment.setId(id);
        comment.setPublishDate(new Timestamp(new Date().getTime()));
        comment.setThumbUp(0);
        commentMapper.insertComment(comment);

        //评论发送消息给文章作者
        String userByArticleId = articleMapper.getUserByArticleId(comment.getArticleId());
        //创建文章作者的品论消息队列
        String noticeQueue = MyEnum.ARTICLE_THUMBSUP.getValue() + userByArticleId;
        //1 创建Rabbit管理
        RabbitAdmin rabbitAdmin = new RabbitAdmin(rabbitTemplate.getConnectionFactory());
        //2声明队列，每个用户都有自己的队列，通过用户id进行区分
        Queue queue = new Queue(noticeQueue, true);
        rabbitAdmin.declareQueue(queue);
        Notice notice = new Notice();
        notice.setId(idWorker.nextId() + "");
        notice.setAction("评论了你");
        notice.setOperatorId(comment.getUserName());
        notice.setReceiverId(userByArticleId);
        notice.setTargetType(MyEnum.ARTICLE_COMMENT_.getValue());
        notice.setTargetId(comment.getId());
        notice.setType("user");
        notice.setState(false);

        String jsonNotice = JSON.toJSONString(notice);
        //将消息发送
        rabbitTemplate.convertAndSend(noticeQueue, jsonNotice);
        return comment;
    }

    @Override
    public void update(Comment comment) {
        commentMapper.updateById(comment);
    }

    @Override
    public void deleteById(String id, String userName) {
        int i = commentMapper.deleteByIdAndUserName(id, userName);
        if (i <= 0){
            throw new MyException(500,"删除异常！");
        }
    }

    @Override
    public PageInfo<Comment> findByarticleId(int current, int size, String articleId) {
        PageHelper.startPage(current, size);
        List<Comment> comments = commentMapper.selectByArticleId(articleId);
        PageInfo<Comment> commentPageInfo = new PageInfo<>(comments);
        return commentPageInfo;
    }

    @Override
    public PageInfo<Comment> findByCeilingCommentId(int current, int size, String ceilingCommentId) {
        PageHelper.startPage(current, size);
        List<Comment> comments = commentMapper.findByCeilingCommentId(ceilingCommentId);
        PageInfo<Comment> commentPageInfo = new PageInfo<>(comments);
        return commentPageInfo;
    }

    @Override
    public void fabulous(String commentId) {
        commentMapper.fabulous(commentId);
    }
}
